package cn.itcast.service.impl;

import cn.itcast.dao.OrderListDao;
import cn.itcast.dao.SetmealDao;
import cn.itcast.entity.OrderQueryPageBean;
import cn.itcast.entity.PageResult;
import cn.itcast.pojo.Address;
import cn.itcast.pojo.Order;
import cn.itcast.pojo.Setmeal;
import cn.itcast.service.OrderListService;
import cn.itcast.util.IdWorker;
import cn.itcast.utils.DateUtils;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class OrderListServiceImpl implements OrderListService {

    @Autowired//预约系统
    private OrderListDao orderListDao;

    @Autowired//套餐系统
    private SetmealDao setmealDao;

    /**
     * 查询所有预约的人
     * @return
     */
    @Override
    public PageResult orderFindAll(OrderQueryPageBean orderQueryPageBean) {
        //开启分页查询
        PageHelper.startPage(orderQueryPageBean.getCurrentPage(),orderQueryPageBean.getPageSize());
        String minTime = orderQueryPageBean.getMinTime();//最小时间
        String maxTime = orderQueryPageBean.getMaxTime();//最大时间
        String orderType = orderQueryPageBean.getOrderType();//预约类型
        Integer payStatus = orderQueryPageBean.getPayStatus();//支付状态
        String queryString = orderQueryPageBean.getQueryString();//身份证、手机号、姓名
        if (payStatus == null){payStatus = 3;}
        String idCard = null;String phoneNumber = null;String name = null;
        System.out.println(queryString);
        if (queryString != null) {
            if (queryString.length() == 18){
                idCard = queryString;
                System.out.println("此数据为身份证");
            } else if (queryString.length() == 11){
                phoneNumber = queryString;
                System.out.println("数据为手机号");
            } else {
                name = queryString;
                System.out.println("剩下来的数据为名称");
            }
        }

        Page<Order> orders = this.orderListDao.orderFindAllCondition(idCard,phoneNumber,name,minTime,maxTime,orderType,payStatus);
        List<Order> orderList = new ArrayList<>();
        for (Order order : orders) {
            Order orderVo;
            Address address = this.orderListDao.queryAddressById(order.getAddressId());
            Setmeal setmeal = this.setmealDao.findById(order.getSetmealId());
            orderVo = order;
            orderVo.setAddress(address);
            orderVo.setSetmeal(setmeal);
            orderVo.setSetmealName(setmeal.getName());
            orderList.add(orderVo);
        }
        log.info(orders + "");
        return new PageResult(orders.getTotal(),orderList);
    }

    /**
     * 新增预约
     * @param param
     */
    @Override
    @Transactional
    public Boolean saveOrder(Map<String, String> param) {
        System.out.println("触发了");
        try {
            Order order = new Order();
            if (param.get("setmealId") == null){
                System.out.println(param.get("setmealId"));
                return false;
            }
            Integer memberId = this.orderListDao.queryMemberById(param.get("phoneNumber"));//查找该用户是否是会员
            order.setMemberId(memberId != null ? memberId : null);//会员id
            order.setOrderDate(DateUtils.parseString2Date(param.get("orderDate")));//预约日期
            order.setOrderType(param.get("orderType"));//预约方式
            order.setOrderStatus(0);//套餐状态 默认定死
            order.setSetmealId(Integer.valueOf(param.get("setmealId")));//体检套餐id
            order.setArrival(0);//是否到诊，一般预约都是不能马上到诊
            order.setPhoneNumber(param.get("phoneNumber"));//预约用户电话
            order.setName(param.get("name"));//预约用户名
            order.setIdCard(param.get("idCard"));//预约身份证号
            order.setBirthday(DateUtils.parseString2Date(param.get("birthday")));//出生日期
            order.setSex(String.valueOf(param.get("sex")));//预约用户性别
            if (param.get("married") != null) {
                order.setMarried(Short.valueOf(param.get("married")));//预约用户婚姻状态
            }
            order.setAddressId(1);//TODO 地址表ID  暂时写死,后续通过其他模块查询
            if (param.get("id") == null){
                IdWorker idWorker = new IdWorker();
                Long id = idWorker.nextId();
                order.setId(String.valueOf(id));//自动生成id
                order.setPayStatus(0);//支付状态表ID
                this.orderListDao.saveOrder(order);
            } else {
                //修改用户信息
                order.setId(param.get("id"));
                this.orderListDao.updateOrder(order);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return true;
    }

    /**
     * 根据用户id查询详细信息
     * @param id
     * @return
     */
    @Override
    public Order queryOrderById(String id) {
        Order order = this.orderListDao.queryOrderById(id);
        return order;
    }

    /**
     * 根据用户id更改就诊状态
     * @param id
     * @param arrival
     * @return
     */
    @Override
    @Transactional
    public Boolean updateOrderArrival(String id, Integer arrival) {
        return this.orderListDao.updateOrderArrival(id,arrival);
    }

}
